//
//  TopAlert.m
//  TopAlert
//
//  Created by roycms on 2016/10/19.
//  Copyright © 2016年 roycms. All rights reserved.
//

#import "TL_TopAlertManager.h"

//#import "TL_ReaderSettingHelper.h"

#define Alert_Duration 1.0f

static TL_TopAlertView *_alertView = nil;

@implementation TL_TopAlertManager

static NSDictionary *_errInfo;
+ (void)showAlertWithError:(NSError *)error defaultText:(NSString * _Nullable)text {
    [self showAlertWithType:TL_TopAlertTypeError alertTitle:text ?: WXLocalizedString(@"请求失败")];
}

+ (void)showAlertWithType:(TL_TopAlertType)type alertTitle:(NSString *)alertTitle
{
    [self showAlertWithType:type alertTitle:alertTitle duration:Alert_Duration];
}

+ (void)showLimitAlertWithType:(TL_TopAlertType)type alertTitle:(NSString *)alertTitle
{
    [self showAlertWithType:type alertTitle:alertTitle duration:Alert_Duration];
}

+ (void)showAlertWithType:(TL_TopAlertType)type alertTitle:(NSString *)alertTitle duration:(CGFloat)duration
{
    [self showAlertWithType:type alertTitle:alertTitle duration:duration completionHandler:nil];
}

+ (void)showAlertWithType:(TL_TopAlertType)type alertTitle:(NSString *)alertTitle completionHandler:(TL_TopAlertDissmissBlock)completionHandler
{
    [self showAlertWithType:type alertTitle:alertTitle duration:Alert_Duration completionHandler:completionHandler];
}

+ (void)showAlertWithType:(TL_TopAlertType)type alertTitle:(NSString *)alertTitle duration:(CGFloat)duration completionHandler:(TL_TopAlertDissmissBlock)completionHandler
{
    if (!alertTitle || alertTitle.length == 0) {
        return;
    }
    if ([_alertView.alertTitle isEqualToString:alertTitle]) {
        return;
    }
    
    if (_alertView) {
        [_alertView hiddenAlertView];
    }
    
    dispatch_async(dispatch_get_main_queue(), ^{
        
        if (type == TL_TopAlertTypeError) {
            
            [TabBarCtrlUtils showToast:alertTitle];
            
        }else{
            
            MJWeakSelf
            _alertView = [[TL_TopAlertView alloc] init];
            _alertView.alertTitle = alertTitle;
            _alertView.alertType = type;
            _alertView.alertDuration = duration;
            _alertView.alertDissmissBlock = ^{
                [weakSelf hiddenAlert];
                if (completionHandler) {
                    completionHandler();
                }
            };
            [_alertView showAlertView];
        }
        
    });
}
    
static UIActivityIndicatorView *activityIndicator;
UIView *maskView;
+ (void)showAlertWithLoading:(TL_TopAlertMaskType)type style:(TL_TopAlertStyle)style {
    if (@available(iOS 13.0, *)) {
        activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleLarge];
    } else {
        activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    }
    activityIndicator.center = [self currentWindow].center;
    
    // 设置动画颜色
    switch (style) {
        case TL_TopAlertStyleLight:
        {
            activityIndicator.color = kWhiteColor;
        }
            break;
        case TL_TopAlertStyleDark:
        {
            activityIndicator.color = [UIColor darkGrayColor];
        }
            break;
    }
    
    // 设置动画的蒙层
    switch (type) {
        case TL_TopAlertMaskTypeNone:
        {
            [[self currentWindow] addSubview:activityIndicator];
        }
            break;
        case TL_TopAlertMaskTypeBlack:

        case TL_TopAlertMaskTypeClear:
        {
            maskView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
            maskView.backgroundColor = type == TL_TopAlertMaskTypeBlack ? [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.5] : [UIColor clearColor];
            [[self currentWindow] addSubview:maskView];
            [[self currentWindow] addSubview:activityIndicator];
        }
            break;
    }
    
    [activityIndicator startAnimating];
}

+ (void)stopAnimating {
    [activityIndicator stopAnimating];
    if (maskView.superview) {
        [maskView removeFromSuperview];
        maskView = nil;
    }
}

+ (void)hiddenAlert
{
    if (_alertView.isShowing) {
        [_alertView hiddenAlertView];
        _alertView = nil;
    }
}

+ (UIWindow *)currentWindow {
    return [UIApplication sharedApplication].windows.firstObject;
}

static UIView *_loadingView;
+ (UIView *)showLoading:(UIView *)rootView {
    UIView *mainView = [[UIView alloc] init];
    _loadingView = mainView;
    mainView.backgroundColor = RGBA(0, 0, 0, 0);
    if (rootView) {
        [rootView addSubview:mainView];
    } else {
        [kMainWindow addSubview:mainView];
    }
    [mainView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(mainView.superview);
    }];
    
    NSMutableArray<UIImage *> *arr = [NSMutableArray array];
    for (int i = 0; i < 47; i++) {
        UIImage *image = [UIImage imageNamed:[@[@"book_loading0.png", @"book_loading1.png", @"book_loading2.png", @"book_loading3.png", @"book_loading4.png", @"book_loading5.png", @"book_loading6.png", @"book_loading7.png", @"book_loading8.png", @"book_loading9.png", @"book_loading10.png", @"book_loading11.png", @"book_loading12.png", @"book_loading13.png", @"book_loading14.png", @"book_loading15.png", @"book_loading16.png", @"book_loading17.png", @"book_loading18.png", @"book_loading19.png", @"book_loading20.png", @"book_loading21.png", @"book_loading22.png", @"book_loading23.png", @"book_loading24.png", @"book_loading25.png", @"book_loading26.png", @"book_loading27.png", @"book_loading28.png", @"book_loading29.png", @"book_loading30.png", @"book_loading31.png", @"book_loading32.png", @"book_loading33.png", @"book_loading34.png", @"book_loading35.png", @"book_loading36.png", @"book_loading37.png", @"book_loading38.png", @"book_loading39.png", @"book_loading40.png", @"book_loading41.png", @"book_loading42.png", @"book_loading43.png", @"book_loading44.png", @"book_loading45.png", @"book_loading46.png"] wx_objectOrNilAtIndex:i]];
        
        image = [image imageByTintColor:[RGB(57, 56, 60) colorWithAlphaComponent:0.75]];
        [arr addObject:image];
    }
    
    YYAnimatedImageView *loadingView = [[YYAnimatedImageView alloc] init];
    loadingView.backgroundColor = [UIColor clearColor];
    loadingView.animationImages = arr;
    loadingView.animationDuration = 2.0;
    [loadingView startAnimating];
    [mainView addSubview:loadingView];
    [loadingView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(mainView);
        make.width.mas_equalTo(90);
        make.height.equalTo(loadingView.mas_width).multipliedBy(150.0 / 240.0);
    }];
    __weak typeof(loadingView) weakView = loadingView;
    [NSTimer scheduledTimerWithTimeInterval:loadingView.animationDuration - 0.1 block:^(NSTimer * _Nonnull timer) {
           if (!weakView.superview) {
               [timer invalidate];
               timer = nil;
           }
           NSArray *t_arr = [[loadingView.animationImages reverseObjectEnumerator] allObjects];
           weakView.animationImages = t_arr;
       } repeats:YES];
    return mainView;
}

+ (void)hideLoading {
    [_loadingView removeFromSuperview];
}

@end
